package com.work.saturn.assets;

import com.acker.simplezxing.activity.CaptureActivity;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.immo.libcommon.base.BaseHeadActivity;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.http.NetworkRequest;
import com.work.saturn.R;
import com.work.saturn.bean.TurnOutBean;
import com.immo.libcommon.constant.RouterUtil;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.immo.libcommon.utils.InputValidationUtils.inputIsEmpty;

/**
 * @author Administrator
 * @content 转出
 * @date 2018/7/11
 */
@Route(path = RouterUtil.ASSETS_TURN_OUT)
public class AssetsTurnOutActivity extends BaseHeadActivity {
    @BindView(R.id.turn_out_account)
    EditText account;
    @BindView(R.id.turn_out_num)
    EditText mTurnOutNum;
    @BindView(R.id.turn_out_dw)
    TextView mTurnOutDw;
    @BindView(R.id.turn_out_bz)
    EditText mTurnOutBz;
    private String name;
    private static final int REQUEST_CODE_PERMISSION_MULTI = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_turn_out);
        ButterKnife.bind(this);
        name = getIntent().getStringExtra("name");
        setTitle(name + "转账");
        setTitleColor(R.color.colorYellow);
        setTitleLeft(R.mipmap.back_finish);
        setTitleRight(null, R.mipmap.acode);
        mTurnOutDw.setText(name);
    }

    @Override
    protected void onRightClick() {
        super.onRightClick();
        getPerMission();
    }

    /**
     * 获取权限
     */
    public void getPerMission() {
        AndPermission.with(this)
                .requestCode(REQUEST_CODE_PERMISSION_MULTI).permission(
                Manifest.permission.CAMERA
        ).callback(this).rationale((requestCode, rationale) -> AndPermission.rationaleDialog(AssetsTurnOutActivity.this, rationale).show()).start();
    }

    @SuppressWarnings("unused")
    @PermissionYes(REQUEST_CODE_PERMISSION_MULTI)
    private void getMultiYes(@NonNull List<String> grantedPermissions) {
        startActivityForResult(new Intent(AssetsTurnOutActivity.this, CaptureActivity.class), CaptureActivity.REQ_CODE);
    }

    @SuppressWarnings("unused")
    @PermissionNo(REQUEST_CODE_PERMISSION_MULTI)
    private void getMultiNo(@NonNull List<String> deniedPermissions) {
        toast("此功能需要获取相机权限");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case CaptureActivity.REQ_CODE:
                switch (resultCode) {
                    case RESULT_OK:
                        account.setText(data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT));
                        break;
                    case RESULT_CANCELED:
                        if (data != null) {
                            account.setText(data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT));
                        }
                        break;
                }
                break;
        }

    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        return inputIsEmpty(this, account.getText().toString(), "转出账号不能为空")
                && inputIsEmpty(this, mTurnOutNum.getText().toString(), "转出金额不能为空");
    }

    /**
     * 转出
     */
    private void TurnOutNet() {
        HashMap<String, String> params = new HashMap<>();
        params.put("tokenName", name);
        params.put("target", account.getText().toString());
        params.put("amount", mTurnOutNum.getText().toString());
        new NetworkRequest((object, data) -> {
            if (object instanceof TurnOutBean) {
                TurnOutBean turnOutBean = (TurnOutBean) object;
                if (turnOutBean.isSuccess()) {
                    toast("转出成功");
                    ARouter.getInstance().build(RouterUtil.MAIN_PAGE).withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                            .navigation();
                } else {
                    toast(turnOutBean.getMessage());
                }
            }
        }).requestPost(BaseUrl.TURNOUT,
                this, JSON.toJSONString(params), TurnOutBean.class, false, false);
    }

    @OnClick(R.id.turn_in_copy)
    public void onViewClicked() {
        if (isCheck()) {
            TurnOutNet();
        }
    }
}
